﻿/**  版本信息模板在安装目录下，可自行修改。
* users.cs
*
* 功 能： N/A
* 类 名： users
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2019-03-15 13:46:41   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Maticsoft.DBUtility;//Please add references
namespace FE.DAL
{
	/// <summary>
	/// 数据访问类:users
	/// </summary>
	public partial class users
	{
		public users()
		{}
		#region  Method


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from users");
			strSql.Append(" where id='"+id+"' ");
			return DbHelperMySQL.Exists(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(FE.Model.users model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.id != null)
			{
				strSql1.Append("id,");
				strSql2.Append("'"+model.id+"',");
			}
			if (model.usernum != null)
			{
				strSql1.Append("usernum,");
				strSql2.Append("'"+model.usernum+"',");
			}
			if (model.Name != null)
			{
				strSql1.Append("Name,");
				strSql2.Append("'"+model.Name+"',");
			}
			if (model.TelNum != null)
			{
				strSql1.Append("TelNum,");
				strSql2.Append("'"+model.TelNum+"',");
			}
			if (model.Callphone != null)
			{
				strSql1.Append("Callphone,");
				strSql2.Append("'"+model.Callphone+"',");
			}
			if (model.DepartmentId != null)
			{
				strSql1.Append("DepartmentId,");
				strSql2.Append(""+model.DepartmentId+",");
			}
			if (model.CreateDate != null)
			{
				strSql1.Append("CreateDate,");
				strSql2.Append("'"+model.CreateDate.ToString("yyyy-MM-dd hh:mm:ss") + "',");
			}
			if (model.gender != null)
			{
				strSql1.Append("gender,");
				strSql2.Append(""+model.gender+",");
			}
			strSql.Append("insert into users(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(FE.Model.users model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update users set ");
			if (model.usernum != null)
			{
				strSql.Append("usernum='"+model.usernum+"',");
			}
			else
			{
				strSql.Append("usernum= null ,");
			}
			if (model.Name != null)
			{
				strSql.Append("Name='"+model.Name+"',");
			}
			else
			{
				strSql.Append("Name= null ,");
			}
			if (model.TelNum != null)
			{
				strSql.Append("TelNum='"+model.TelNum+"',");
			}
			else
			{
				strSql.Append("TelNum= null ,");
			}
			if (model.Callphone != null)
			{
				strSql.Append("Callphone='"+model.Callphone+"',");
			}
			else
			{
				strSql.Append("Callphone= null ,");
			}
			if (model.DepartmentId != null)
			{
				strSql.Append("DepartmentId="+model.DepartmentId+",");
			}
			else
			{
				strSql.Append("DepartmentId= null ,");
			}
			if (model.CreateDate != null)
			{
				strSql.Append("CreateDate='"+model.CreateDate.ToString("yyyy-MM-dd hh:mm:ss")+"',");
			}
			else
			{
				strSql.Append("CreateDate= null ,");
			}
			if (model.gender != null)
			{
				strSql.Append("gender="+model.gender+",");
			}
			else
			{
				strSql.Append("gender= null ,");
			}
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where id='"+ model.id+"' ");
			int rowsAffected=DbHelperMySQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from users ");
			strSql.Append(" where id='"+id+"' " );
			int rowsAffected=DbHelperMySQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from users ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public FE.Model.users GetModel(string id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  ");
			strSql.Append(" id,usernum,Name,TelNum,Callphone,DepartmentId,CreateDate,gender ");
			strSql.Append(" from users ");
			strSql.Append(" where id='"+id+"' " );
			FE.Model.users model=new FE.Model.users();
			DataSet ds=DbHelperMySQL.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public FE.Model.users DataRowToModel(DataRow row)
		{
			FE.Model.users model=new FE.Model.users();
			if (row != null)
			{
				if(row["id"]!=null)
				{
					model.id=row["id"].ToString();
				}
				if(row["usernum"]!=null)
				{
					model.usernum=row["usernum"].ToString();
				}
				if(row["Name"]!=null)
				{
					model.Name=row["Name"].ToString();
				}
				if(row["TelNum"]!=null)
				{
					model.TelNum=row["TelNum"].ToString();
				}
				if(row["Callphone"]!=null)
				{
					model.Callphone=row["Callphone"].ToString();
				}
				if(row["DepartmentId"]!=null && row["DepartmentId"].ToString()!="")
				{
					model.DepartmentId=int.Parse(row["DepartmentId"].ToString());
				}
				if(row["CreateDate"]!=null && row["CreateDate"].ToString()!="")
				{
					model.CreateDate=DateTime.Parse(row["CreateDate"].ToString());
				}
				if(row["gender"]!=null && row["gender"].ToString()!="")
				{
					model.gender=int.Parse(row["gender"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,usernum,Name,TelNum,Callphone,DepartmentId,CreateDate,gender ");
			strSql.Append(" FROM users ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM users ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperMySQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from users T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
		#region  MethodEx

		#endregion  MethodEx
	}
}

